/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.webservice.model;

import com.ipcc.manager.entity.IPCC_EXTENSIONS;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class ExtensionWSModel {

//    public static void main(String[] args) {
//        try {
//            String strOrgXML = getLinesInTemplate("");
//            System.out.println("Original XML:" + strOrgXML);
//
//            strOrgXML = strOrgXML.replaceAll("<user_id>", "1000");
//            strOrgXML = strOrgXML.replaceAll("<password>", "123456");
//            strOrgXML = strOrgXML.replaceAll("<extension>", "1000");
//            strOrgXML = strOrgXML.replaceAll("<group>", "mygroup");
//
//            System.out.println("Modded XML:" + strOrgXML);
//        } catch (Exception ex) {
//            Logger.getLogger(ExtensionWSModel.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
    public static String getLinesInTemplate(String fileName) throws Exception {
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String strLineSeparator = System.getProperty("line.separator");
        String a = resource.getString("urlCreateXML");
        String separator = System.getProperty("file.separator");
        File file = new File(a + separator + fileName);        
        String strReturn = "";
        String line;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));            
            while ((line = br.readLine()) != null) {
                strReturn += line + strLineSeparator;
            }                        
            return strReturn;
        } finally {
            if( br != null) br.close();
        }
    }

    public static void createXML(String content, String filename) throws Exception {
        String separator = System.getProperty("file.separator");
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String path = resource.getString("urlCreateXML");
        BufferedWriter bw = null;
        try {
            File file = new File(path + separator + filename);
            bw = new BufferedWriter(new FileWriter(file));
            bw.append(content);
            bw.flush();
        } finally {
            bw.close();
        }

    }

    public static ArrayList<String> getLinesInXML(String fileName) throws Exception {
        ArrayList<String> list = new ArrayList<String>();
        String separator = System.getProperty("file.separator");
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String path = resource.getString("urlCreateXML");
        BufferedReader br = null;
        try {
            File file = new File(path + separator + fileName);
            br = new BufferedReader(new FileReader(file));
            while (br.ready()) {
                String line = br.readLine();
                list.add(line);
            }
            return list;
        } finally {
            br.close();
        }
    }

    public static void writeXML(ArrayList<String> lines, String filename) throws Exception {
        String separator = System.getProperty("file.separator");
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String path = resource.getString("urlCreateXML");
        BufferedWriter bw = null;
        try {
            File file = new File(path + separator + filename);
            bw = new BufferedWriter(new FileWriter(file));
            for (int i = 0; i < lines.size(); i++) {
                bw.append(lines.get(i) + "\n");
                bw.flush();
            }
        } finally {
            bw.close();
        }
    }

    public static boolean checkFileExists(String filename) {
        String separator = System.getProperty("file.separator");
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String path = resource.getString("urlCreateXML");
        File file = new File(path + separator + filename);
        if (file.exists()) {
            return true;
        }
        return false;
    }

    public static void editPassInXML(String newPwd, String fileName) throws Exception {
        ArrayList<String> list = new ArrayList<String>();
        list = getLinesInXML(fileName);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).contains("password") && !list.get(i).contains("vm-password")) {
                list.set(i, "      <param name=\"password\" value=\"" + newPwd + "\"/>");
            }
        }
        writeXML(list, fileName);
    }
    
    public static boolean delXMLFile(String fileName){
        String separator = System.getProperty("file.separator");
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String path = resource.getString("urlCreateXML");
        File file = new File(path + separator + fileName);
        if(checkFileExists(fileName)){
            if(file.delete()) return true;
        }
        return false;
    }
//    public static void main(String[] args) throws Exception {
//        IPCC_EXTENSIONS ie = new IPCC_EXTENSIONS();
//        ie.setExtension("113");
//        ie.setExtension_group("Nhon");
//        ie.setPassword("thangcan");
//        ArrayList<String> list = new ArrayList<String>();
//        try {
//            list = getLinesInTemplate("/templateXML.txt");
//            for (int i = 0; i < list.size(); i++) {
//                if(list.get(i).contains("-extension-")){
//                    String tg = list.get(i);
//                    tg.replaceAll("-extension-", ie.getExtension());
//                    list.set(i, tg);
//                }
//                if(list.get(i).contains("-password-")){
//                    String tg = list.get(i);
//                    tg.replaceAll("-password-", ie.getPassword());
//                    list.set(i, tg);
//                }
//                
//                if(list.get(i).contains("-group-")){
//                    String tg = list.get(i);
//                    tg.replaceAll("-group-", ie.getExtension_group() );
//                    list.set(i, tg);
//                }                
//            }
//            ExtensionWSModel.writeXML(list, ie.getExtension() + ".xml");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
